const { contextBridge, ipcRenderer } = require('electron/renderer')

contextBridge.exposeInMainWorld('versions', {
  node: () => process.versions.node,
  chrome: () => process.versions.chrome,
  electron: () => process.versions.electron,
  ping: () => ipcRenderer.invoke('ping'),
  app: () => process.env.npm_package_version,
})

contextBridge.exposeInMainWorld('clipboard', {
  readText: () => ipcRenderer.invoke('clipboard:readText'),
  writeText: (text) => ipcRenderer.invoke('clipboard:writeText', text)
})


contextBridge.exposeInMainWorld('electronAPI', {
  setTitle: (title) => ipcRenderer.send('set-title', title),
  openFile: () => ipcRenderer.invoke('dialog:openFile'),
  onUpdateCounter: (callback) => ipcRenderer.on('update-counter', (_event, value) => callback(value)),
  counterValue: (value) => ipcRenderer.send('counter-value', value),
  startDrag: (fileName) => ipcRenderer.send('ondragstart', fileName),
  requestWindowFocus: () => ipcRenderer.send('request-window-focus')
})



// ipcRenderer.on('port', e => {
//   // 接收到端口，使其全局可用。
//   window.electronMessagePort = e.ports[0]

//   window.electronMessagePort.onmessage = messageEvent => {
//     // 处理消息
//     console.log(messageEvent)
//   }
// })

contextBridge.exposeInMainWorld('darkMode', {
  toggle: () => ipcRenderer.invoke('dark-mode:toggle'),
  system: () => ipcRenderer.invoke('dark-mode:system')
})
